5325. Stand in order

 

At a session of the Summer Computer School, n schoolchildren are preparing for a parade. They need to stand in an orderly fashion. Upon a given command, they all line up and start counting. The first student in line says “first”, the second says “second”, and so on. Write the script for this process.

 

Input. One integer n (1 ≤ n ≤ 1000).

 

Output. Print all the numbers from 1 to n in a single line.

 

Sample input 1

Sample output 1

1

1

 

 

Sample input 2

Sample output 2

4

1 2 3 4

 

 

SOLUTION

loops

 

Algorithm analysis

Print the numbers from 1 to n using a loop.

 

Algorithm realization

Read the input value of n.

 

scanf("%d",&n);

 

Print the numbers from 1 to n in a single line, separated by spaces.

 

for(i = 1; i <= n; i++)

  printf("%d ",i);

 

Terminate the line with a newline character ‘\n.

 

printf("\n");

 

Algorithm realization – recursion

 

#include <stdio.h>

 

int i, n;

 

void run(int n)

{

  if (n == 0) return;

  run(n-1);

  printf("%d ",n);

}

 

int main(void)

{

  scanf("%d",&n);

  run(n);

  printf("\n");

  return 0;

}

 

Java realization

 

import java.util.*;

 

class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    for(int i = 1; i <= n; i++)

      System.out.print(i + " ");

    con.close();

  }

}

 

Python realization

Read the input value of n.

 

n = int(input())

 

Print the numbers from 1 to n in a single line, separated by spaces.

 

for i in range(1,n+1):

  print(i, end = " ")